<?php
/*
 * Created on 2013-8-9
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include_once("../configure/configure.php");						//--> global var
include_once("../admin/configure/admin.config.inc.php");			//-- admin var

//include_once("utils/ServerUtils.php");

$filepath = "/Library/WebServer/sites/hohomm-v1/test/logo.jpg";

echo "dir: " . dirname ($filepath). "<br>";
echo "file: " . basename ($filepath). "<br>";

//exit;

$imageinfo = getImageSize($filepath, &$info);

$imgWidth 	= $imageinfo[0];
$imgHeight	= $imageinfo[1];
$imaType	= $imageinfo[2];

echo "is gif : " . isAnimatedGif($filepath);
//print_r($info);
exit;

$w = 60;
$h = 60;

echo "type: " . $imaType. "<br>";

$outfilepath = dirname ($filepath) ."/img_$w" ."x". "$h" . "-3.gif";

echo "outfilepath: " . $outfilepath. "<br>";

//exit;

try {  
	if($imgWidth > $imgHeight){
		$command = "/opt/local/bin/convert $filepath -resize x$h -gravity center -extent $w". "x" . "$h $outfilepath";
		echo "c1: $command <br>";
		
		exec($command, $status);
	}else{
		$command = "/opt/local/bin/convert $filepath -resize $w". "x" . " -gravity center -extent $w". "x" . "$h $outfilepath";
		
		echo "c2: $command <br>";
		exec($command, $status);
	}
		///usr/bin/convert  
//		exec("/opt/local/bin/convert $filepath -resize x60 t2.gif", $status);  
		
		print_r($status);
		echo "Exit status code of command is $status";
}catch (Exception $e){  
	echo $e->getMessage();
}


//try {  
////	if ( copy($_FILES['uploadfile']['tmp_name'], $uploadfile) ){  	  
//		///usr/bin/convert  
//		exec("/usr/bin/convert test.gif -resize x60 t2.gif");  
//		
////		return "/kkyoo/rooms/icoImg/{$subdir}/".$name;  
////	}  
//}catch (Exception $e){  
//	echo $e->getMessage();  
//} 


//echo "host: " . ServerUtils::gethost(true);

//header("content-type:image/gif"); 
//header("Location: ../images/content/0/02/00/200.gif");
//exit();
	
//$im = imagecreate(400,30); 
//$black = imagecolorallocate($im,0,0,0); 
//$white = imagecolorallocate($im,255,255,255); 
//imageline( $im,200,15,215,15,$white); 
//imagestring($im, 5, 4, 10, "This is a Gif", $white); 
//
//imagegif($im);

//$fn=fopen("./test.gif","r"); 
//fpassthru($fn); 

//$im = imagecreatefromgif('test.gif');
//imagegif($im);
//
//imagedestroy($im); 

//function LoadGif($imgname)
//{
//    $im = @imagecreatefromgif($imgname); /* Attempt to open */
//    if(!$im) { /* See if it failed */
//        $im = imagecreatetruecolor(150, 30); /* Create a blank image */
//        $bgc = imagecolorallocate($im, 255, 255, 255);
//        $tc = imagecolorallocate($im, 0, 0, 0);
//        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
//        /* Output an errmsg */
//        imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
//    }
//    return $im;
//}

function isAnimatedGif($filename) {

    $fp = fopen($filename, 'rb');

    $filecontent = fread($fp, filesize($filename));

    fclose($fp);

    return strpos($filecontent, chr(0x21) . chr(0xff) . chr(0x0b) . 'NETSCAPE2.0') === FALSE ? 0 : 1;

}

?>
